﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Database
{
   public class LoaiLinhKien
    {
        private int loailk;

        public int Loailk
        {
            get { return loailk; }
            set { loailk = value; }
        }
        private string tenloailk;

        public string Tenloailk
        {
            get { return tenloailk; }
            set { tenloailk = value; }
        }
        public LoaiLinhKien()
        {
        }
        public LoaiLinhKien(int loailk, string tenloailk)
        {
            this.loailk = loailk;
            this.tenloailk = tenloailk;
        }
        public static DataSet DSLoaiLK()
        {
            DataSet ds = new DataSet();
            string query = "select loailk,tenloailk from loailinhkien";
            ConnectToDatabase db = new ConnectToDatabase();
            SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
            sda.Fill(ds);
            return ds;
        }
        public DataSet DSLoaiLinhKien()
        {
            DataSet ds = new DataSet();
            string query = "select loailk,tenloailk from loailinhkien";
            ConnectToDatabase db = new ConnectToDatabase();
            SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
            sda.Fill(ds);
            return ds;
        }
        public DataTable DanhSachLoaiLK()
        {
            DataTable ds = new DataTable();
            try
            {
                string query = "select * from loailinhkien";
                ConnectToDatabase db = new ConnectToDatabase();
                SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
                sda.Fill(ds);
            }
            catch
            {
                ds = null;
            }
            return ds;
        }
        public DataTable DSHangSanXuatTheoLoai(int loailk)
        {
            DataSet ds = new DataSet();
            try
            {
                string query = "select * from loailinhkien where loailk={0}";
                query = string.Format(query, loailk);
                ConnectToDatabase db = new ConnectToDatabase();
                SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
                sda.Fill(ds);
            }
            catch
            {
                ds = null;
            }
            return ds.Tables[0];
        }
        public bool ThemLoaiLK(string tenloailk)
        {
            bool kq = true;

            try
            {
                string query = "insert into LoaiLinhKien(tenloailk) values('{0}')";
                query = String.Format(query, tenloailk);
                ConnectToDatabase db = new ConnectToDatabase();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                return cmd.ExecuteNonQuery() > 0;
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public bool XoaLoaiLinhKien(int loailk)
        {
            bool kq = true;

            try
            {
                string query = "Delete loailinhkien where loailk='{0}'";
                query = String.Format(query,loailk);
                ConnectToDatabase db = new ConnectToDatabase();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                cmd.ExecuteNonQuery();
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public bool CapNhatLoaiLK(int loailk, string tenloailk)
        {
            bool kq = true;

            try
            {
                string query = "update LoaiLinhKien set tenloailk='{0}' where loailk={1}";
                query = String.Format(query, tenloailk, loailk);
                ConnectToDatabase db = new ConnectToDatabase();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                return cmd.ExecuteNonQuery() > 0;
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public LoaiLinhKien ThongTinLoaiLK(int loailk)
        {
            string query = "select * from loailinhkien where loailk=" + loailk;
            ConnectToDatabase db = new ConnectToDatabase();
            SqlCommand sc = new SqlCommand(query, db.GetConnect());
            SqlDataReader reader = sc.ExecuteReader();
            LoaiLinhKien llk = new LoaiLinhKien();
            while (reader.Read())
            {
                llk.loailk= reader.GetInt32(0);
                llk.tenloailk = reader.GetString(1);
            }
            return llk;
        }
    }
}
